package com.erp.erp_project.controller;

import com.erp.erp_project.core.AjaxResult;
import com.erp.erp_project.core.annotation.Permission;
import com.erp.erp_project.mapper.CraftMapper;
import com.erp.erp_project.mapper.PlanMapper;
import com.erp.erp_project.model.Craft;
import com.erp.erp_project.model.Equip;
import com.erp.erp_project.model.Plan;
import com.erp.erp_project.model.Work;
import com.erp.erp_project.service.PlanService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@AllArgsConstructor
@RequestMapping("plan")
public class PlanController {
    private final PlanService planService;
    private final CraftMapper craftMapper;
    private final PlanMapper planMapper;

    @GetMapping("list")
    public String list() {
        return "/plan/list";
    }

    //展示
    @PostMapping("list")
    @ResponseBody
    public AjaxResult<PageInfo<Plan>> listAll(Plan plan, Integer page, Integer limit, Integer type, String va) {
        PageHelper.startPage(page, limit);
        PageInfo<Plan> pageInfo = new PageInfo<>(planService.list(plan));
        return new AjaxResult<PageInfo<Plan>>(pageInfo);
    }

    //添加
    @GetMapping("add")
    @Permission("生产计划添加")
    public String get(Model model) {
        List<Craft> crafts = craftMapper.list();
        model.addAttribute("icraft", crafts);
        return "plan/add";
    }

    //添加
    @PostMapping("add")
    @ResponseBody
    @Permission("生产计划添加")
    public AjaxResult<String> post(Plan plan) {
        planMapper.add(plan);
        return new AjaxResult<String>("success");
    }

    //编辑
    @GetMapping("edit")
    @Permission("生产计划修改")
    public String getEdit(Plan plan, Model model) {
        List<Craft> craftList = craftMapper.list();
        model.addAttribute("craftList", craftList);
        model.addAttribute("plan", planMapper.selectById(plan.getId()));
        return "plan/edit";
    }

    //edit
    @PostMapping("edit")
    @ResponseBody
    @Permission("生产计划修改")
    public AjaxResult<String> postEdit(Plan plan) {
        planMapper.update(plan);
        return new AjaxResult<String>("修改成功");
    }

    //工艺信息展示
    @GetMapping("info")
    @Permission("生产计划修改")
    public String getInfo(Plan plan, Model model) {
        Craft craft = new Craft();
        craft = craftMapper.selectByIcraft(plan.getCraft());
        model.addAttribute("icraft",craft);
        return "craft/info";
    }
    //工艺编辑
    @PostMapping("infoEdit")
    @ResponseBody
    @Permission("生产计划修改")
    public AjaxResult<String> infoEdit(Craft craft){
        craftMapper.update(craft);
        return new AjaxResult<String>("修改成功");
    }
    //删除
    //删除
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    @Permission("生产计划删除")
    public AjaxResult<String> del(@PathVariable Integer id){
        planMapper.delete(id);
        return new AjaxResult<String>("删除成功");
    }
}
